﻿using System;
using System.Web;
using System.Configuration;
using System.Text.RegularExpressions;

namespace Ajax.Tools.Security
{
    /// <summary>
    /// CheckSN 的摘要说明。
    /// </summary>
    public class ZhengBan
    {

        #region 验证序列号
        /// <summary>
        /// 验证序列号
        /// </summary>
        /// <returns></returns>
        public bool Check()
        {
            bool OutPut = false;
            string IP = HttpContext.Current.Request.ServerVariables["LOCAL_ADDR"];
            if (ConfigurationManager.AppSettings["SN"] == MakeSN())
            {
                OutPut = true;
            }
            else//如果是192.168.*.*的ip就通过验证
            {
                string[] s = IP.Split('.');

                if (s[0] + s[1] == "192168" || IP == "127.0.0.1")
                {
                    OutPut = true;
                }
                else
                {
                    OutPut = false;
                }
                string[] ss = HttpContext.Current.Request.ServerVariables["http_host"].Split('.');

                foreach (string str in ss)
                {
                    if (str.ToLower() == "Ajax")
                    {
                        OutPut = true;
                    }
                }
            }
            return OutPut;
        }

        /// <summary>
        /// 检查商业用户
        /// </summary>
        /// <returns></returns>
        public bool CheckB()
        {
            bool OutPut = false;
            string IP = HttpContext.Current.Request.ServerVariables["LOCAL_ADDR"];
            if (ConfigurationManager.AppSettings["SN"] == MakeSNB("checkSN"))
            {
                OutPut = true;
            }

            return OutPut;
        }
        #endregion

        /// <summary>
        /// 得到识别码！
        /// </summary>
        /// <returns></returns>
        public string ShiBieMa()
        {
            string str = "";
            try
            {
                str = Key.HDVal();//硬盘C盘序列号
            }
            catch
            {
                str = "JW*#I@(";
            }
            str += HttpContext.Current.Request.ServerVariables["APPL_PHYSICAL_PATH"];
            return Hash.SHA1(str);
        }

        /// <summary>
        /// 制造序列号－－免费
        /// </summary>
        /// <returns></returns>
        internal string MakeSN()
        {
            string str = Hash.MD5(ShiBieMa());
            str = Hash.SHA1(str);

            for (int i = 1; i < 6; i++)
            {
                str = str.Remove(i * 5, 1);
                str = str.Insert(i * 6, "-");
            }
            return str;
        }

        /// <summary>
        /// 制造序列号－－付费
        /// </summary>
        /// <returns></returns>
        public string MakeSNB(string PSW)
        {
            string str = Hash.MD5(ShiBieMa()).Substring(0, 16);
            str = Hash.SHA1(str);

            for (int i = 1; i < 6; i++)
            {
                str = str.Remove(i * 5, 1);
                str = str.Insert(i * 6, "-");
            }
            if (PSW == "checkSN")
                return "Ajax" + str;
            else
                return "";
        }

        /// <summary>
        /// 检查版权信息
        /// </summary>
        /// <param name="str"></param>
        public void BanQuan(string str, int Type)
        {
            if (Type == 1)
            {
				Regex reg = new Regex(@"(Ajax 企业网站内容管理系统)", RegexOptions.IgnoreCase);
                if (reg.IsMatch(str) == false)
                {
                    ErrClass.Redirect("只有付费用户才能除去版权信息和链接。");
                }
            }
            else
            {
                Regex reg = new Regex(@"(Ajax 企业网站内容管理系统)", RegexOptions.IgnoreCase);
                if (reg.IsMatch(str) == false)
                {
                    ErrClass.Redirect("只有付费用户才能除去版权信息和链接。");
                }
            }
        }
    }
}